"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const GameUtils_1 = __importDefault(require("../game/GameUtils"));
const Logger_1 = __importDefault(require("../utils/Logger"));
const Room_1 = require("./Room");
/**
 * 游戏总控
 */
class HallMgr {
    constructor() {
        this.users = new Map(); //全部玩家
        this.rooms = new Set(); //匹配房合集
        this.cipherRooms = new Set(); //密码房合集
        this.room_number = 0; //创建房间的个数，同时也是room的id值
    }
    /** 匹配 */
    match(user) {
        for (let room of this.rooms) {
            if (room.checkAddPlayer()) {
                room.addPlayer(user);
                return;
            }
        }
        let room = new Room_1.Room(++this.room_number);
        room.addPlayer(user);
        this.rooms.add(room);
    }
    /** 创建或加入房间 */
    createOrjoinRoom(user, cipher) {
        for (let room of this.cipherRooms) {
            if (room.checkCipherRoomAddPlayer(cipher)) {
                room.addPlayer(user);
                return;
            }
        }
        let room = new Room_1.Room(++this.room_number, cipher);
        room.addPlayer(user);
        this.cipherRooms.add(room);
    }
    /** 观战 */
    watchingRoom(user) {
        for (let room of this.cipherRooms) {
            if (room.state == Room_1.RoomState.GAMEING) {
                room.addPlayer(user);
                return;
            }
        }
        let json = JSON.stringify(GameUtils_1.default.getObj("watchFail", { "message": "没有对局，无法观战" }, false));
        user.connection.sendText(json);
    }
    /** 聊天 */
    chat(obj) {
        var json = JSON.stringify(GameUtils_1.default.getObj("chat", obj.data, false));
        Logger_1.default.log("send:" + json);
        for (let value of this.users.values()) {
            value.connection.sendText(json);
        }
    }
    /** 在线人数，这里只返回在大厅的人数，在游戏中的不管 */
    updateOnline() {
        let num = 0;
        for (let user of this.users.values()) {
            if (user.room == null || user.room.state == Room_1.RoomState.MATCHING) {
                num++;
            }
        }
        var json = JSON.stringify(GameUtils_1.default.getObj("updateOnline", { "number": num }, false));
        Logger_1.default.log("send: " + json);
        for (let value of this.users.values()) {
            value.connection.sendText(json);
        }
    }
}
exports.default = new HallMgr();
